import os
import sys
from pathlib import Path
import shutil

def batch_rename(directory, prefix="file"):
    try:
        path = Path(directory)
        for idx, file in enumerate(path.iterdir()):
            if file.is_file():
                new_name = f"{prefix}_{idx}{file.suffix}"
                file.rename(path / new_name)
                print(f"Renamed: {file.name} -> {new_name}")
    except Exception as e:
        print(f"[Error] 批量重命名失败: {e}")

def clean_txt_files(directory):
    try:
        path = Path(directory)
        for file in path.glob("*.txt"):
            with file.open("r", encoding="utf-8") as f:
                lines = f.readlines()
            cleaned = [line.strip() for line in lines if line.strip()]
            with file.open("w", encoding="utf-8") as f:
                f.write("\n".join(cleaned))
            print(f"Cleaned: {file.name}")
    except Exception as e:
        print(f"[Error] 清洗失败: {e}")

